﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Diploma.Models;
using Diploma.Models.Entities;

namespace DiplomaWeb.Models.CompaniesAndLecturers
{
    public class CompanyViewModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public int LevelId { get; set; }
        public int MaxStudents { get; set; }
        public int CurrentStudents { get; set; }

        public CompanyViewModel() {}

        public CompanyViewModel(Company c)
        {
            ToViewModel(c);
        }

        public CompanyViewModel(LecturerInCompanyEntity model)
        {
            ToViewModel(model);
        }

        private void ToViewModel(LecturerInCompanyEntity company)
        {
            Id = company.CompanyId;
            Name = company.CompanyName;
            LevelId = company.LevelId;
            var companiesForQualificationLevel = DataManager.Instance.CompaniesRepository.Get(company.CompanyId).CompaniesForQualificationLevels.FirstOrDefault(r => r.QualificationLevelId == company.LevelId);
            MaxStudents = companiesForQualificationLevel != null ? companiesForQualificationLevel.MaxSudents.HasValue ? companiesForQualificationLevel.MaxSudents.Value : 0 : 0;
            CurrentStudents = DataManager.Instance.StudentsRepository.GetStudentsForCompany(company.LevelId, company.CompanyId).Count();
        }

        private void ToViewModel(Company company)
        {
            Id = company.Id;
            Name = company.DisplayName;
        }
    }
}